﻿using FRMS.DAL;
using FRMS.Model;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace FRMS.TimedTask
{
    public class DataCleanService : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            //int day = Convert.ToInt32(CommVars.Configuration.GetSection("RetainingTime").Value);
            //RecordDAL da = new RecordDAL();
            //bool result = da.TimedDeleteDeviceRecoed(day);
            //bool result2 = da.TimedDeleteLadderRecoed(day);
            try
            {
                VoucherDAL voucherDAL = new VoucherDAL();
                List<VoucherInfo> vouchers = voucherDAL.QueryOverVisit();
                foreach (var item in vouchers)
                {
                    try
                    {
                        JLLssueinfoDel info = JLVoucherDel(item);
                        string re = HttpClientPost("http://" + item.DeviceIP + ":8011/Request", info);
                        JLLssueinfoReturn jLLssueinfoReturn = JsonConvert.DeserializeObject<JLLssueinfoReturn>(re);
                        if (jLLssueinfoReturn.Name == "personListResponse" && jLLssueinfoReturn.Code == 1)
                        {
                            DelVoucher(item);
                        }
                        else if (jLLssueinfoReturn.Name == "personListResponse" && jLLssueinfoReturn.Data.Result == -21 && jLLssueinfoReturn.Code == 1110)
                        {
                            DelVoucher(item);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }

                DeviceDAL deviceDAL = new DeviceDAL();
                List<DeviceInfo> deviceInfos = deviceDAL.QueryOnLine();
                foreach (var item in deviceInfos)
                {
                    try
                    {
                        TimeSynchronization timeSynchronization = new TimeSynchronization();
                        timeSynchronization.Name = "timeSynchronizationRequest";
                        timeSynchronization.Session = item.Session;
                        timeSynchronization.UUID = item.DeviceUuid;
                        timeSynchronization.TimeStamp = GetTimeStamp();
                        TimeSynchronizationData timeSynchronizationData = new TimeSynchronizationData();
                        timeSynchronizationData.TimeMode = 1;
                        timeSynchronizationData.LocalTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        timeSynchronization.Data = timeSynchronizationData;
                        string re = HttpClientPost("http://" + item.DeviceIP + ":8011/Request", timeSynchronization);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
            catch
            {

            }
        }

        public JLLssueinfoDel JLVoucherDel(VoucherInfo voucher)
        {
            JLLssueinfoDel jLLssueinfo = new JLLssueinfoDel();
            jLLssueinfo.Name = "personListRequest";
            jLLssueinfo.UUID = voucher.DeviceUuid;
            jLLssueinfo.Session = voucher.Session;
            jLLssueinfo.TimeStamp = GetTimeStamp();
            JLLssueinfoDelData jLLssueinfoData = new JLLssueinfoDelData();
            jLLssueinfoData.Action = "deletePerson";
            jLLssueinfoData.PersonType = 2;
            jLLssueinfoData.PersonId = voucher.UUID;
            jLLssueinfo.Data = jLLssueinfoData;
            return jLLssueinfo;
        }

        public int GetTimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt32(ts.TotalSeconds);
        }

        public string HttpClientPost(string url, object datajson)
        {
            HttpClient httpClient = new HttpClient();//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            HttpContent httpContent = new JsonContent(datajson);
            //请求
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }

        public ResponseMessage DelVoucher(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();

            VoucherDAL da = new VoucherDAL();
            bool result = da.DeleteVoucher(voucherInfo);
            if (result)
            {
                OwnerDAL ownerDAL = new OwnerDAL();
                OwnerInfo ownerInfo = new OwnerInfo();
                ownerInfo.OwnerType = 4;
                ownerInfo.ID = voucherInfo.OwnerID;
                ownerDAL.DeleteOwner(ownerInfo);
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "删除凭证失败";
            }

            return message;
        }
    }
}
